// packageB/pages/package/task_details_manage/pfef2/temp_package_infos/inspect_set/index.js
const mybehavior = require("../../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
    btn_show:true,
    row_data: {},
    options: [],
    sign_module: [],
    fc_list: [],
    wl_list: [],
    gysp_list: [],
    pz_list: [],
    yjjsz_list: [],
    fc_list1: [],
    wl_list1: [],
    gysp_list1: [],
    pz_list1: [],
    yjjsz_list1: [],
  },
  setKeyInput(e) {
    console.log('setKeyInput', e);
    let search_word = e.detail
    this.inspectUserSearch(search_word)
  },
  pickerShow(e) {
    this.setData({
      options: [],
      btn_show:e.detail
    })
  },


  inspectUserSearch(search_word) {
    const paload = {
      name: search_word,

    }
    console.log(paload);
    let url = this.data.baseUrl + "/user_manage/get_user_by_name"
    this.post(url, paload).then(res => {
      console.log(res);
      if (res.code == 20000) {
        let array = res.data
        let res_arr = []
        for (let i = 0; i < array.length; i++) {
          res_arr.push(array[i].name + '-' + array[i].key)

        }
        this.setData({
          options: res_arr
        })


      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options: []
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  arrConfirm(e) {
    console.log("arrConfirm", e)
    let key = e.currentTarget.dataset.key
    let set_key = e.currentTarget.dataset.set_key
    let array = e.detail
    let num_arr = []
    for (let i = 0; i < array.length; i++) {
      num_arr.push(array[i].split('-')[1])
    }
    this.setData({
      [key]: array,
      [set_key]: num_arr,
    })
    console.log(this.data);

  },
  cancelBtn() {
    wx.navigateBack()
  },

  submitBtn() {
    if (this.checkValue()) {
      let sign_module = [
        {
          current_process: "分厂审批",
          current_involved: this.data.fc_list1
        },
        {
          current_process: "物流审批",
          current_involved: this.data.wl_list1
        },
        {
          current_process: "工艺审批",
          current_involved: this.data.gysp_list1
        },
        {
          current_process: "批准",
          current_involved: this.data.pz_list1
        },
      ]


      let row = this.data.row_data
      const paload = {
        action: 'agree',
        key: row.task_id,
        reason: '设置签审团队',
        sign_module: sign_module,
        receivers: this.data.yjjsz_list1,
      }
      console.log(paload);
      let url = this.data.baseUrl + "/temp_packgae_manage/before_standard_inspect"
      this.post(url, paload).then(res => {
        console.log(res);
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageB/pages/package/task_manager/task_manager',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }



  },
  checkValue() {
    if (this.data.fc_list.length == 0) {
      wx.showToast({
        title: '请选择分厂审批人员！',
        icon: 'none'
      })
      return false
    } else if (this.data.wl_list.length == 0) {
      wx.showToast({
        title: '请选择物流审批人员！',
        icon: 'none'
      })
      return false
    } else if (this.data.gysp_list.length == 0) {
      wx.showToast({
        title: '请选择工艺审批人员！',
        icon: 'none'
      })
      return false
    } else if (this.data.pz_list.length == 0) {
      wx.showToast({
        title: '请选择批准人员！',
        icon: 'none'
      })
      return false
    } else if (this.data.yjjsz_list.length == 0) {
      wx.showToast({
        title: '请选择邮件接收者！',
        icon: 'none'
      })
      return false
    } else {
      return true

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("传过来的数据", data)
      self.setData({
        row_data:data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})